<template>
    <div>
        <input type="button" value="添加" @click="router.push('/houlenoadd')" class="btn btn-info"/>
        <input type="button" value="批量删除"  class="btn btn-danger" @click="delall"/>
        <input type="button" value="批量修改"  class="btn btn-primary" @click="update"/>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" v-model="checked" @click="checkall"/>全选</td>
                    <td>房型名称</td>
                    <td>房间号码</td>
                    <td>房间描述</td>
                    <td>状态</td>
                </tr>

                <tr v-for="item in data.list" :key="item.Id">
                    <td><input type="checkbox" v-model="ids"/></td>
                    <td>{{ item.HouleId }}</td>
                    <td>{{ item.NumNo }}</td>
                    <td>{{ item.Miaoshuno }}</td>
                    <td>{{ item.State?"上架":"下架" }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted} from 'vue'
import axios from 'axios';
import {useRouter,useRoute} from 'vue-router'
import { idText } from 'typescript';
const router=useRouter();
const route=useRoute();

let data:any=reactive({
    list:[]
})
let checked=ref(false);
let ids=ref([]);

onMounted(()=>{
    getlist();
})

const checkall=()=>{
    checked.value=!checked.value;
    if(checked.value==true){
        ids.value=data.list.map((item:any)=>{
            return item.id;
        })
    }
    else{
        return ids.value=[];
    }
}

const delall=()=>{
    if(!confirm("确认删除?")){
        return;
    }
    axios({
        url:'/api/HouleNo/Delall',
        method:'delete',
        params:{id:ids.value.toString()}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            getlist();
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const update=()=>{
    if(!confirm("确认修改?")){
        return;
    }
            alert('修改成功');
            getlist();
}


const getlist=()=>{
    axios({
        url:'/api/HouleNo/GetHouleno',
        method:'get',
    })
    .then(res=>{
        console.log(res);
        data.list=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped>
input[type="button"]{
    margin-left: 10px;
}
</style>